const codeCollection = require("../models/codeCollection")
const code = require("../models/code")
const demo = require("../models/demo")

var express = require('express');
var router = express.Router();

const formidable = require('formidable');

//返回图片
router.post('/', (req, res, next) => {
    var form = new formidable.IncomingForm();
    form.parse(req, function (err, fields, files) {
        if (err) {
            res.json({ 'state': false })
        } else {
          codeCollection.find({
            _id: fields.id
          }, async (error, doc) => {
            if (error) {
                res.json({ 'state': false, 'msg': error });
            } else {
              // console.log(doc)
              if(doc.length===0){
                res.json({ 'state': false, 'msg': "no collection!" });
              }
              if(doc[0].codeDemoId!==''){
                let old = await demo.findById(doc[0].codeDemoId)
                res.json({
                  'state': true,
                  'msg': {
                    new: doc,
                    old: old
                  }
                })
              } else if (doc[0].demoId!==''){
                let old = await code.findById(doc[0].demoId)
                res.json({
                  'state': true,
                  'msg': {
                    new: doc,
                    old: old
                  }
                })
              }
            }
          })
        }

    });

});

module.exports = router;